<?php

class Book_IndexController extends Zend_Controller_Action
{
    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $book = new Book_Model_DbTable_Book();
        $bookData= $book->fetchAll();
        //$this->view->books = $bookData;
        
        $paginator = Zend_Paginator::factory($bookData->toArray());
        $paginator->setItemCountPerPage(2);        
        $paginator->setPageRange(3);
        $currentPage = $this->_request->getParam('page',1);
        $paginator->setCurrentPageNumber($currentPage);        

        $this->view->paginator = $paginator;
    }

    public function addAction()
    {
        $form = new Book_Form_Book();
        $form->submit->setLabel('Add');
        $this->view->form = $form;
        
        if($this->getRequest()->isPost())
        {            
            $formData = $this->getRequest()->getPost();            
            if($form->isValid($formData))
            {                                
                $author = $form->getValue('author');
                $title = $form->getValue('title');
                $price = $form->getValue('price');
                
                //Upload File
                $upload = new Zend_File_Transfer_Adapter_Http();
                $upload->setDestination(APPLICATION_PATH . '/../public/images/');
                
                $fileName= $upload->getFileName();
                $ext= end(explode('.', $fileName));
                $rename= uniqid().'.'.$ext;               
                $upload->addFilter('Rename', APPLICATION_PATH . '/../public/images/' . $rename);                
                
                $files = $upload->getFileInfo();                                                                                                                 
                                                                
                try {
                    // upload received file(s)
                    $upload->receive();                    
                } catch (Zend_File_Transfer_Exception $e) {
                    $e->getMessage();
                }              
                
                if(file_exists(APPLICATION_PATH .'/../public/images/'. $rename)) {
                    $file_upload= $rename;
                } else {
                    $file_upload= null;
                }
                
                $book = new Book_Model_DbTable_Book();
                $book->addBook($author, $title, $price, $file_upload);
                $this->_helper->redirector('index');
            }
            else
            {
                $form->populate($formData);
            }
        }
    }
    
    public function editAction()
    {
        // action body
        $form = new Book_Form_Book();
        $form->submit->setLabel('Save');
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $id = (int)$form->getValue('id');
                $author = $form->getValue('author');
                $title = $form->getValue('title');
                $price = $form->getValue('price');
                
                //Upload File
                $upload = new Zend_File_Transfer_Adapter_Http();
                $upload->setDestination(APPLICATION_PATH . '/../public/images/');
                
                $fileName= $upload->getFileName();
                $ext= end(explode('.', $fileName));
                $rename= uniqid().'.'.$ext;               
                $upload->addFilter('Rename', APPLICATION_PATH . '/../public/images/' . $rename);                
                
                $files = $upload->getFileInfo();                                                                                                                 
                                                                
                try {
                    // upload received file(s)
                    $upload->receive();                    
                } catch (Zend_File_Transfer_Exception $e) {
                    $e->getMessage();
                }              
                
                if(file_exists(APPLICATION_PATH .'/../public/images/'. $rename)) {
                    $file_upload= $rename;
                } else {
                    $file_upload= null;
                }
                
                $book = new Book_Model_DbTable_Book();
                $book->updateBook($id, $author, $title, $price, $file_upload);

                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $id = $this->_getParam('id', 0);
            if ($id > 0) {
                $book = new Book_Model_DbTable_Book();
                $book_data= $book->getBook($id);
                $form->populate($book_data);
                $this->view->book= $book_data;
            }
        }
    }
    
    public function deleteAction()
    {
        // action body
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('id');
                $book = new Book_Model_DbTable_Book();
                $book->deleteBook($id);

                $this->_helper->redirector('index');
            } else {                
                $this->_helper->redirector('index');
            }
        }
        else {
            $id = $this->_getParam('id', 0);
            $book = new Book_Model_DbTable_Book();
            $this->view->book = $book->getBook($id);
        }
    }
}

